package com.start_eternity.oasissafewalk.controller;
import com.start_eternity.oasissafewalk.comment.ErrorCode;
import com.start_eternity.oasissafewalk.comment.ResultUtils;
import com.start_eternity.oasissafewalk.pojo.PageBean;
import com.start_eternity.oasissafewalk.pojo.Parent;
import com.start_eternity.oasissafewalk.comment.Result;
import com.start_eternity.oasissafewalk.service.ParentService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * author:ZhangT
 * Date:2024 / 03 / 14 / 9:18
 * Description:
 */
@Slf4j
@RestController
@RequestMapping(value = "parent",method = {RequestMethod.GET, RequestMethod.POST})
public class ParentController {

    @Resource
    private ParentService parentService;
    //分页查询
    @GetMapping("/page")
    public Result<PageBean> findPages(@RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10")Integer pageSize) {
        PageBean pageBean = parentService.queryPage(page, pageSize);
        if (pageBean != null) {
            return ResultUtils.success(pageBean);
        }
        return ResultUtils.error(ErrorCode.NO_DATA);
    }

    //添加个人用户
    @PostMapping("/save")
    public Result<Integer> save(@RequestBody Parent parent) {
        int rows = parentService.inserting(parent);
        if (rows > 0) {
            return ResultUtils.success(rows);
        }
        return ResultUtils.error(ErrorCode.FAIL_INSERT);
    }

    //删除用户
    @GetMapping("/delete")
    public Result<Integer> del(Integer pId) {
        int rows = parentService.delById(pId);
        if (rows > 0) {
            return ResultUtils.success(rows);
        }
        return ResultUtils.error(ErrorCode.FAIL_DELETE);
    }
    //修改信息
    @PostMapping("/update")
    public Result<Integer> update(@RequestBody Parent parent) {
        int rows = parentService.updating(parent);
        if (rows > 0) {
            return ResultUtils.success(rows);
        }
        return ResultUtils.error(ErrorCode.FAIL_UPDATE);
    }


    //根据id查询个人id
    @GetMapping("/query")
    public Result<Parent> queryById(@RequestParam Integer pId) {
        Parent parent = parentService.queryById(pId);
        if (parent != null) {
            return ResultUtils.success(parent);
        }
        return ResultUtils.error(ErrorCode.NO_DATA);
    }

    //  查询家长关联的学生信息
    @GetMapping("/queryListByPId")
    public Result<List<Parent>> queryListByPId(@RequestParam Integer pId) {
        List<Parent> parents = parentService.queryListByPId(pId);
        if (!parents.isEmpty()) {
            return ResultUtils.success(parents);
        }
        return ResultUtils.error(ErrorCode.NO_DATA);
    }

    //根据家长id和学生id确定orderNo内容
    @GetMapping("/queryOrderNo")
    public Result<String> queryOrderNo(@RequestParam Integer pId,@RequestParam Integer sId) {
        String orderNo = parentService.selDetails(pId,sId);

        if (!orderNo.isEmpty()) {
            return ResultUtils.success(orderNo);
        }
        return ResultUtils.error(ErrorCode.NO_DATA);
    }
}
